<template>
  <div class="container">
    <el-dialog
      :title="title"
      :visible.sync="showDialog"
      width="30%"
      :before-close="handleClose"
    >
      <!-- 表单 -->
      <el-form
        ref="primaryForm"
        label-position="top"
        class="demo-ruleForm"
        label-width="100px"
        style="margin:0 60px;"
        :model="primaryForm"
        :rules="rules"
      >
        <el-form-item
          label="楼宇名称"
          prop="name"
        >
          <el-input v-model="primaryForm.name" placeholder="请输入楼宇名称" />
        </el-form-item>
        <el-form-item
          label="楼宇层数"
          prop="floors"
        >
          <el-input v-model.number="primaryForm.floors" type="number" placeholder="请输入楼宇层数" :min="1" />
        </el-form-item>
        <el-form-item
          label="在管面积"
          prop="area"
        >
          <el-input v-model.number="primaryForm.area" step="0.01" placeholder="请输入在管面积">
            <template slot="append">m²</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="物业费"
          prop="propertyFeePrice"
        >
          <el-input v-model.number="primaryForm.propertyFeePrice" placeholder="请输入物业费">
            <template slot="append">元/m²</template>
          </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitPrimaryForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addBuilding, editBackfillBuilding, editBuilding } from '@/api/buildind'
export default {
  name: 'AddBuilding',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '新增楼宇'
    },
    primaryId: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      primaryForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      rules: {
        name: [{ required: true, message: '楼宇名称不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '楼宇名称请填写3-15位的字符', trigger: 'blur' }
        ],
        floors: [{ required: true, message: '楼宇层数不能为空', trigger: 'blur' },
          { type: 'number', message: '楼宇层数必须为数字值', trigger: 'blur' }],
        area: [{ required: true, message: '在管面积不能为空', trigger: 'blur' },
          { type: 'number', message: '在管面积必须为数字值', trigger: 'blur' }],
        propertyFeePrice: [{ required: true, message: '物业费不能为空', trigger: 'blur' },
          { type: 'number', message: '物业费必须为数字值', trigger: 'blur' }]
      }
    }
  },

  methods: {
    // 关闭模态框
    handleClose() {
      this.$emit('update:showDialog', false)
    },
    // 提交表单
    async submitPrimaryForm() {
      await this.$refs.primaryForm.validate()
      if (this.title === '新增楼宇') {
        await addBuilding(this.primaryForm)
        this.$message.success('新增成功')
      } else {
        const { name, floors, area, propertyFeePrice } = this.primaryForm
        await editBuilding({ name: name, floors: floors, area: area, propertyFeePrice: propertyFeePrice, id: this.primaryId })
        this.$message.success('编辑成功')
      }
      // 通知父组件更新数据
      this.$emit('updatePrimaryForm')
      this.$refs.primaryForm.resetFields()
      this.handleClose()
    },
    // 编辑
    async editBackfillBuilding() {
      const res = await editBackfillBuilding(this.primaryId)
      this.primaryForm = res.data
    }
  }
}
</script>
<style scoped>

  .text {
    font-size: 14px;
    margin: 30px;
  }

</style>
